<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\MustVerifyEmail;

class EnsureEmailIsVerified
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if ($request->user() && $request->user() instanceof MustVerifyEmail && !request()->user()->hasVerifiedEmail()
        && !request()->is('email/*','logout')) {
            return $request->expectsJson()
                ? abort(403,'Your email address is not verified'):
                redirect()->route('verification.notice');
        }

        return $next($request);
    }
}
